/*
 * 合同变更: 1.新建合同变更及变更明细 2.查看变更内容 
 	查看 合同变更id  查看 type=1 ; 编辑type=2
    新建 合同id
    编辑 合同变更id 
 */
public with sharing  class ContractChangeController

{

    public ContractChangeController(){
        
    }
    /*
   	public String type {set;get;}///合同变更查看标识
    
    public ContractChange__c cc {get;set;}//合同变更
            
    public List<ContractChangeListBean> cclbList{get;set;}///合同变更明细
    
    public List<ProductBean> pbList{get;set;}//搜索产品
    
    public Id cid  {get;set;}
    
    public User u ;///当前用户
    
    //public List<ContractChangeListBean> cpcclList{get;set;}//合同产品--->变更明细
    
    public List<ContractChangeListBean> tempList{get;set;}///临时

    public List<ContractChangeListBean> parentTempList {get;set;}//分子机构总部的合同产品
    
    public Boolean isSuccess {get;set;}//是否保存成功
    
    public Boolean isSpecialAccount {get;set;}//是否为专票客户

    public Boolean isSystemAccount {get;set;}///是否为第三方体系分支客户

    public String  parentconId ;//总公司有效合同ID



    ////===================搜索条件=======================
    public String name {get;set;}//名称已经
    
    public String category0 {get;set;}//主分类
    
    public String category1 {get;set;}//一级分类
    
    public String brand {get;set;}//品牌
    
    public String serial {get;set;}//规格
    
    public String typeName {get;set;}//产品类型
    ///======================从合同产品筛选条件====================================
    public String tempName {get;set;}//名称
    
    public String tempCategory1 {get;set;}//一级分类
    
    public String tempCategory0 {get;set;}//二级分类
    
    public String tempBrand {get;set;}//品牌
    
    public String tempSerialNo {get;set;}// 货号

    public String tempSpec {get;set;}//规格

    public String tempTypeName {get;set;}//产品类型
    ///==========================从分支机构总部筛选条件================================
    public String sysName {get;set;}//品名

    public String sysBrand {get;set;}//品牌

    public String sysSpec {get;set;}//规格

    public String sysSerialNo {get;set;}//货号

    public String sysTypeName {get;set;}//产品类型

    public String sysCategory1 {get;set;}//一级分类

    ///========================下拉框=========================
    public List<SelectOption> cate0Opts { get; private set; }//
    
    public List<SelectOption> cate1Opts { get; private set; }

    public String contractId;

    public Set<Id> productSet ;///合同中存在的产品 不允许重产品中选出

    //public List<SelectOption> tempCategory0opts { get; private set; }//一级分类选项列表
    
    //public List<SelectOption> tempCategory1opts { get; private set; }//二级分类选项列表

    public Boolean isApporal {get;set;}///s是否在审批中

    public String sealflag ;//封存标记;

    public String concode ;//合同编码
    
    public ContractChangeController()
    {
       //cpcclList = new List<ContractChangeListBean>();
       pbList = new List<ProductBean>();
       cclbList = new  List<ContractChangeListBean>();
       tempList = new List<ContractChangeListBean>();
        parentTempList = new List<ContractChangeListBean>();
        productSet = new Set<Id>();


       cid = System.currentPageReference().getParameters().get('id');
       type = System.currentPageReference().getParameters().get('type');
        u = [Select SubCompany__c from User where Id = : UserInfo.getUserId()];
        
    }
    ///设置下拉框
    private void setCateOptions(){
    	cate0Opts = new List<SelectOption>();
        cate0Opts.add(new SelectOption('', System.Label.Option_None));
        for (String str : System.Label.Product_Category0.split(',', -1)) {
            cate0Opts.add(new SelectOption(str, str));
        }

        cate1Opts = new List<SelectOption>();
        cate1Opts.add(new SelectOption('', System.Label.Option_None));
        for (String str : System.Label.Product_Category1.split(',', -1)) {
            cate1Opts.add(new SelectOption(str, str));
        }
    }
    ///初始
    public void init(){
         cc = new ContractChange__c();
    	if(cid.getSobjectType()==ContractChange__c.SobjectType){//查看或编辑
        	getContractChanges();
            
        }else if(cid.getSobjectType()==Contract__c.SobjectType){//新建
            ///新建合同变更
            cc.Contact__c = cid;
            cc.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('Dealer','ContractChange__c');
            
        	//getContractProducts(cid);
            contractId = cid;
        }
        ///设置下拉框
        setCateOptions();

        ///合同中存在的产品
        for(ContractProduct__c cp: [select Product__c from ContractProduct__c where Contract__c =:contractId]){
            if(cp.Product__c != null )productSet.add(cp.Product__c);
        }
        ///判断是否为第三方体系的分支机构 如果是第三方机构的分支机构 则在合同变更的时候带去第三方机构的体系价格
        Contract__c con = [select Account__r.System__c,Account__r.ParentId,CompanyControls__c  from Contract__c where Id =:contractId];
         ///获取封存标记
        Map<String,String> cm = getPicklistValues('Contract__c','Corpcode__c');
        concode = cm.get(con.CompanyControls__c);
         sealflag = 'Y-'+concode;
        isSystemAccount = con.Account__r.ParentId != null;
        ///第三方体系总部 
       if(con.Account__r.ParentId != null){
            List<Contract__c> cons = [select Id from Contract__c where Account__c =:con.Account__r.ParentId and Stopped__c != true and Status__c = '已生效' Order by CreatedDate desc limit 1];
            parentconId = (cons != null && cons.size() > 0 )? cons[0].Id : null;
            getSystemContractProduct();
       } 

       


    }
       //获得PickList的值
    public static Map<String, String> getPicklistValues(String sObjectName, String sFieldName){
        Map<String, String> picValues= new Map<String, String>();
        Map<String, Schema.SObjectType> sObjectDescribeMap = Schema.getGlobalDescribe();
        if (sObjectDescribeMap.containsKey(sObjectName)) {
            Map<String, Schema.SobjectField> sObjectFieldsMap = 
               sObjectDescribeMap.get(sObjectName).getDescribe().fields.getMap();
            if (sObjectFieldsMap.containsKey(sFieldName)) {
                Schema.DescribeFieldResult sObjectFieldDescribe = sObjectFieldsMap.get(sFieldName).getDescribe();
                List<Schema.PicklistEntry> lPickEntryList = sObjectFieldDescribe.getPicklistValues();
                for (Schema.PicklistEntry tPicklistEntry : lPickEntryList) {
                    if(tPicklistEntry.isActive() == true){
                        picValues.put(tPicklistEntry.getLabel(),tPicklistEntry.getValue());
                    }
                   
                }
            } else {
                //TODO 不存在此字段的执行
            }
        } else {
            // TODO 不存在此sObject的执行
        }
        return picValues;
    }
    
    ///获取合同变更明细
   private void getContractChanges(){
       //合同变更
       
      	cc = [select Id,Name,Contact__c,Contact__r.Name,Status__c from ContractChange__c where id =:cid];
       //
        isApporal = (cc.Status__c== '审批中' || cc.Status__c== '审批成功') ? true :false;

		Schema.DescribeSobjectResult m = ContractChangeList__c.sObjectType.getDescribe();
        Map<String, Schema.SObjectField> fieldMap = m.fields.getMap();
        
        String sql = 'select ';
        String fields = '';
        for (String field : fieldMap.keySet()) {
            if (fields.length() > 0) {
                fields += ', '; 
            }
            fields += field;
        }
        sql += fields;
        sql += ' ,ContractProduct__r.Product__r.Category0__c,ContractProduct__r.Product__r.Category1__c,Product__r.Name,Product__r.zl__c from ContractChangeList__c where ContactChange__c = \'' + cc.Id + '\'';
        for (ContractChangeList__c ccl : Database.query(sql)) {
            cclbList.add(new ContractChangeListBean(ccl));
        }
       contractId = cc.Contact__c;
      // getContractProducts(cc.Contact__c);
    }
    ///----------------------------------搜索产品--------------------------------
    public void search(){
        System.debug('==='+brand);
    	pbList.clear();
        String whereStr = '';
        List<Product__c> plist = new List<Product__c>();
        if (String.isBlank(name) == false) {
            whereStr += ' and Name like \'%' + name + '%\'';
        }
        if (String.isBlank(brand) == false) {
            whereStr += ' and  Brand__c like \'%' + brand + '%\'';
        }
        if (String.isBlank(serial) == false) {
            whereStr += ' and  SerialNo__c like \'%' + serial + '%\'';
        }
        if (String.isBlank(category0) == false) {
            whereStr += ' and  Category0__c = \'' + category0 + '\'';
        }
        if (String.isBlank(category1) == false) {
            whereStr += ' and  Category1__c = \'' + category1 + '\'';
        }
        if (String.isBlank(typeName) == false) {
            whereStr += ' and  TypeName__c like \'%' + typeName + '%\'';
        }
               
        plist = Database.query(this.soqlStr() + whereStr + ' limit 500');
       // 获取价格信息
        Set<Id> pidList = new Set<Id>();
        for (Product__c p : plist) {
            if(productSet.contains(p.Id))continue;
            if(!(p.SealCompany__c != null && p.SealCompany__c.contains(sealflag)))pidList.add(p.Id);

        }
         //如果是第三方体系机构 获取总部的售价
        Map<ID,Decimal> salesPriceMap = getSystemPrice(pidList);
        Map<Id, Price__c> priceMap = getPriceMap(pidList);
        for (Product__c p : plist) {
            if(productSet.contains(p.Id))continue;
            if(priceMap.get(p.Id) == null)continue;
            if(!(p.SealCompany__c != null && p.SealCompany__c.contains(sealflag)))pbList.add(new ProductBean(p, priceMap.get(p.Id),salesPriceMap.get(p.Id)));
        }
    }
    ///获取总公司价
    private Map<Id,Decimal> getSystemPrice(Set<Id>pids){
        
        Map<Id,Decimal>priceMap = new Map<Id,Decimal>();
        if(parentconId == null)return priceMap;

        for (ContractProduct__c cp : [select Id,SalesPrice__c,Product__c  from ContractProduct__c where 
            Contract__c =:parentconId and DelContractProduct__c != true]) {
            priceMap.put(cp.Product__c,cp.SalesPrice__c);
        }
        return priceMap;

    }
    private String soqlStr() {
        Schema.DescribeSobjectResult m = Product__c.sObjectType.getDescribe();
        Map<String, Schema.SObjectField> fieldMap = m.fields.getMap();
        
        String sql = 'select ';
        String fields = '';
        for (String field : fieldMap.keySet()) {
            if (fields.length() > 0) {
                fields += ', '; 
            }
            fields += field;
        }
        sql += fields;
        sql += ' from Product__c where Name != null and  PriceCompany__c  like \'%' +concode+ '%\'  ';

        return sql;
    }
      ///------------------------------获取价格----------------------------------------
      private Map<Id,Price__c> getPriceMap(Set<Id> pids){
            Map<Id, Price__c> priceMap = new Map<Id, Price__c>();
                for (Price__c pr : [select Id,name, Product__c,Product__r.bggsjhdj__c, MarketPrice__c, HospitalManagerPrice__c, HospitalBottomPrice__c, CompanyBottomPrice__c,
                            AreaDealerPrice__c, InvoiceDealerPrice__c, RepairCost__c, InstCost__c ,LastMonthAveragePrice__c,Taxrate__c  
                                    from Price__c where Product__c in : pids and Company__c = :u.SubCompany__c]) {
                    priceMap.put(pr.Product__c, pr);
                }
          return priceMap;

      }
    ///----------------交换选择的产品---------------------
    public void exchange(){
        
    	// 去掉已勾选的报价产品
        List<ContractChangeListBean> stayList = new List<ContractChangeListBean>();
        for (ContractChangeListBean ccl : cclbList) {
            //System.debug('===d'+ccl.productName + ccl.chk);
            if (ccl.chk == false) {
                stayList.add(ccl);
            }
        }
        cclbList.clear();
        cclbList.addAll(stayList);
        //System.debug('==='+cclbList);
        // 收集已选报价产品的产品Id
        Set<Id> existPid = new Set<Id>();
        //收集已勾选的合同产品
        Set<Id> existCPid = new Set<Id>();
        
        for (ContractChangeListBean ccl : cclbList) {
            existPid.add(ccl.product);
            existCPid.add(ccl.ContractProduct);
        }
         
        // 加入已勾选的产品
        for (ProductBean p : pbList) {
            if (p.chk == true && existPid.contains(p.rec.Id) == false) {
                existPid.add(p.rec.Id);
                cclbList.add(new ContractChangeListBean(p.rec,p.pr,p.salesprice));
                p.chk = false;
            }
        }
        
       // System.debug('===2'+cclbList);
        // 加入已勾选的合同产品
        for(ContractChangeListBean ccl :tempList){
        	if(ccl.chk == true && existCPid.contains(ccl.ContractProduct) == false){
                ccl.chk = false;
                existCPid.add(ccl.ContractProduct);
                existPid.add(ccl.product);
            	cclbList.add(ccl.clone());
             
            }
        }

         for(ContractChangeListBean ccl :parentTempList){
            if(ccl.chk == true && existCPid.contains(ccl.ContractProduct) == false && !existPid.contains(ccl.product)){
                ccl.chk = false;
               
                cclbList.add(ccl.clone());
             
            }
        }
        

        //System.debug('===1'+cclbList);
         //tempCategoryOpt(cpcclList);
    }
    public String genCPSql(String conId){
          ///查询合同产品
        Schema.DescribeSobjectResult m = ContractProduct__c.sObjectType.getDescribe();
        Map<String, Schema.SObjectField> fieldMap = m.fields.getMap();
        
        String sql = 'select ';
        String fields = '';
        for (String field : fieldMap.keySet()) {
            if (fields.length() > 0) {
                fields += ', '; 
            }
            fields += field;
        }
        sql += fields;
        sql += ', product__r.SealCompany__c,product__r.ProductLineName__c,Product__r.Category0__c,Product__r.Category1__c,Product__r.Brand__c,Product__r.TypeName__c,Product__r.zl__c,Product__r.Name from ContractProduct__c where Contract__c = \'' + conId + '\' and DelContractProduct__c != true';
        return sql ;
    }

    public void getSystemContractProduct(){
        parentTempList.clear();
        ///现有合同产品
        Set<Id>  prodSet= new Set<Id>();
        for (ContractProduct__c cp : [select Id,Product__c from ContractProduct__c where Contract__c =: contractId]) {
            prodSet.add(cp.Product__c);
        }
        String sql = genCPSql(parentconId);
        Map<Id,Id>pidMap = new Map<Id,Id>();

         String whereStr = '';
         
        if (String.isBlank(sysName) == false) {
            whereStr += ' and Product__r.Name like \'%' + sysName + '%\'';
        }
        if (String.isBlank(sysBrand) == false) {
            whereStr += ' and  Product__r.Brand__c like \'%' + sysBrand + '%\'';
        }
        if (String.isBlank(sysSerialNo) == false) {
            whereStr += ' and  Product__r.SerialNo__c like \'%' + sysSerialNo + '%\'';
        }
        if (String.isBlank(sysTypeName) == false) {
            whereStr += ' and  Product__r.TypeName__c like \'%' + sysTypeName + '%\'';
        }
        if (String.isBlank(sysSpec) == false) {
            whereStr += ' and  Product__r.Spec__c like \'%' + sysSpec + '%\'';
        }
        if (String.isBlank(sysCategory1) == false) {
            whereStr += ' and  Product__r.Category1__c like \'%' + sysCategory1 + '%\'';
        }
       System.debug(sql+ whereStr);
        List<ContractProduct__c> resList = Database.query(sql+ whereStr + ' limit 500');
        for (ContractProduct__c cp : resList) {
            if(!prodSet.contains(cp.Product__c)&&(!(cp.Product__r.SealCompany__c != null && cp.Product__r.SealCompany__c.contains(sealflag))))pidMap.put(cp.Product__c,cp.Id);
        }
     //获取价格体系
        Map<Id,Price__c> priceMap = getPriceMap(pidMap.keySet());
        for (ContractProduct__c cp : resList) {
           Price__c pr = null;
            if(priceMap.containsKey(cp.Product__c))pr = priceMap.get(cp.Product__c);
            if(!prodSet.contains(cp.Product__c)&&(!(cp.Product__r.SealCompany__c != null && cp.Product__r.SealCompany__c.contains(sealflag))))parentTempList.add(new ContractChangeListBean(cp,pr,cp.SalesPrice__c,true));

        }

    }

  
    public void searchCP(){
        tempList.clear();
       
        String sql = genCPSql(contractId);
        String whereStr = '';
         if (String.isBlank(tempName) == false) {
            whereStr += ' and Product__r.Name like \'%' + tempName + '%\'';
        }
        if (String.isBlank(tempBrand) == false) {
            whereStr += ' and  Product__r.Brand__c like \'%' + tempBrand + '%\'';
        }
        if (String.isBlank(tempSerialNo) == false) {
            whereStr += ' and  Product__r.SerialNo__c like \'%' + tempSerialNo + '%\'';
        }
        if (String.isBlank(tempTypeName) == false) {
            whereStr += ' and  Product__r.TypeName__c like \'%' + tempTypeName + '%\'';
        }
        if (String.isBlank(tempSpec) == false) {
            whereStr += ' and  Product__r.Spec__c like \'%' + tempSpec + '%\'';
        }
        if (String.isBlank(tempCategory1) == false) {
            whereStr += ' and  Product__r.Category1__c like \'%' + tempCategory1 + '%\'';
        }

        List<ContractProduct__c> resList = Database.query(sql+ whereStr + ' limit 200');
        Map<Id,Id>pidMap = new Map<Id,Id>();
        for (ContractProduct__c cp : resList) {
            if(!(cp.Product__r.SealCompany__c != null && cp.Product__r.SealCompany__c.contains(sealflag)))pidMap.put(cp.Product__c,cp.Id);
        }
        //获取价格体系
        Map<Id,Price__c> priceMap = getPriceMap(pidMap.keySet());
        //如果是第三方体系机构 获取总部的售价
        Map<ID,Decimal> salesPriceMap = getSystemPrice(pidMap.keySet());


        Integer i = 0;
        for (ContractProduct__c cp : resList) {
            Price__c pr = null;
            Decimal salesprice = null;
            if(priceMap.containsKey(cp.Product__c))pr = priceMap.get(cp.Product__c);
            if(salesPriceMap.containsKey(cp.Product__c))salesprice = salesPriceMap.get(cp.Product__c);
            //System.debug('===合同产品价格'+pr.name+'==='+pr.HospitalManagerPrice__c);
            ///cpcclList.add(new ContractChangeListBean(cp,pr));

            if(!(cp.Product__r.SealCompany__c != null && cp.Product__r.SealCompany__c.contains(sealflag)))tempList.add(new ContractChangeListBean(cp,pr,salesprice,false));
            

        }
    }
    
    //-----------------------------保存-------------------------------------
    public void save(){
        
       if(type =='2'){
       		Savepoint sp = Database.setSavepoint();
        try{
            ////删除原来的
        	if(cid.getSobjectType()==ContractChange__c.SobjectType){
            	List<ContractChangeList__c> delList = [select Id from ContractChangeList__c where ContactChange__c =:cid];
                if(delList.size() > 0) delete delList;
                System.debug('===删除'+delList);
            }
            
            System.debug('==='+cc);
           if(cc.Id == null ){
            	insert cc;
            }
            ///保存新建的
            List<ContractChangeList__c> newCclList = new List<ContractChangeList__c>();
            if(cclbList.size() > 0){
                for(ContractChangeListBean cclb:cclbList){
                	ContractChangeList__c ccl = new ContractChangeList__c();
                    ccl.ContractProduct__c = cclb.ContractProduct;
                    ccl.AddProduct__c = cclb.AddProduct;
                    ccl.DelProduct__c = cclb.DelProduct;
                    ccl.AdjustPrice__C = cclb.AdjustPrice ;
                	
                     ccl.InvoiceDealerPrice__c = cclb.invoiceDealerPrice;
                     ccl.product__c = cclb.product;
                     ccl.productLine__c = cclb.productLine;
                     ccl.instCost__c = cclb.instCost;
                     ccl.code__c = cclb.code;
                     ccl.SalesPrice__c = cclb.SalesPrice;
                     ccl.AdjustSalesPrice__c = cclb.AdjustSalesPrice;
                     ccl.Size__c = cclb.Size;
                     ccl.ItemNumber__c = cclb.ItemNumber;
                     ccl.Type__c = cclb.Type;
                     ccl.Brand__c = cclb.Brand;
                     ccl.AreaDealerPrice__c = cclb.AreaDealerPrice;
                     ccl.swjhdj__c = cclb.swjhdj;
                     ccl.MarketPrice__c = cclb.MarketPrice;
                     ccl.Number__c = cclb.num;
                     ccl.Model__c = cclb.Model;
                    // ccl.Name = cclb.productName;
                     ccl.ContactChange__c = cc.Id;
                   // System.debug('保存价格:'+cclb.productName + ':'+cclb.CompanyBottomPrice+ ':'+cclb.HospitalBottomPrice+ ':'+cclb.HospitalManagerPrice);
                    ccl.CompanyBottomPrice__c = cclb.CompanyBottomPrice;
                    ccl.HospitalBottomPrice__c = cclb.HospitalBottomPrice;
                    ccl.HospitalManagerPrice__c = cclb.HospitalManagerPrice;
                    ccl.DiscountRate__c = cclb.discountRate;
                    ccl.SystemPrice__c = cclb.parentSalesPrice;
                    ccl.IsFromSystem__c = cclb.isFromSystem;
                    newCclList.add(ccl);
                }
              if(newCclList.size() > 0) insert newCclList; 
              isSuccess = true;
            }
        }catch(Exception e){
        	Database.rollback(sp);
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, e.getMessage()));
            
        }
       } 
    	
    }
     public class ContractChangeListBean {
    	
        public Decimal	invoiceDealerPrice {get;set;}//报单经销商价
        public String   product {get;set;}//产品
        public String   productLine{get;set;}//产品线
        public Decimal  instCost{get;set;}//成本价
        public String   code{get;set;}//存货代码
        public Decimal   SalesPrice{get;set;}//单价
        public Boolean    AdjustPrice{get;set;}//是否调价
        public Decimal    AdjustSalesPrice{get;set;}//调整价格
        public String    Company{get;set;}//公司
        public String   Size{get;set;}//规格
        public String    ContractProduct{get;set;}//合同产品
        public String   ItemNumber{get;set;}//货号
        public String    Type{get;set;}//类型
        public String    Brand{get;set;}//品牌
        public Decimal    AreaDealerPrice{get;set;}//区域经销商价
        public Boolean    DelProduct{get;set;}//是否删除合同产品
        public Decimal   swjhdj{get;set;}//生物公司计划单价
        public Decimal    MarketPrice{get;set;}//市场报价
        public Decimal    num{get;set;}//数量
        public Decimal    Taxrate{get;set;}//税率
        public Boolean    AddProduct{get;set;}//是否添加合同产品
        public String    Model{get;set;}//型号
        public Decimal   CompanyBottomPrice{get;set;}//公司低价
        public Decimal   HospitalBottomPrice{get;set;}//销售员底价
        public Decimal   HospitalManagerPrice{get;set;}//终端指导价
        public Decimal 	 discountRate{get;set;}//减价率
        public Decimal   parentSalesPrice {get;set;}//总部的售价
                
        public Boolean  isFromSystem {get;set;}//是否来自总部
        public String zl{get;set;}//装量
        public String  productName {get;set;}//合同产品名称
        public String category0 {get;set;}///主分类
        public String category1 {get;set;}//一级分类
        public String typeName {get;set;}//产品分类
         
        public Boolean chk {get;set;}//是否选中
        public Boolean isFromProduct {get;set;} //是否为产品 如果是产品则 可以填写单价   如果不是则不能 只能填写调整价格
        
         
        public ContractChangeListBean(ContractProduct__c cp,Price__c pr,Decimal sprice,Boolean isSys){
        	invoiceDealerPrice = pr != null ?pr.InvoiceDealerPrice__c:null;
            product = cp.product__c;
            productLine = cp.product__r.Category1__c;
            MarketPrice = pr != null ?pr.MarketPrice__c:null;
            instCost = pr != null ? pr.instCost__c:null;

            code = cp.code__c;
            SalesPrice = cp.SalesPrice__c;
            AdjustPrice = !isSys;
            Company = cp.Company__c;
            Size = cp.Size__c;
            ContractProduct = cp.Id;
            ItemNumber = cp.ItemNumber__c;
            Type = cp.Type__c;
            Brand = cp.Brand__c;
            DelProduct = false;
            num = cp.Number__c;
            AddProduct = isSys;
            Model = cp.Model__c;
            productName= cp.Product__r.Name;
            category0 = cp.Product__r.Category0__c;
            category1 = cp.Product__r.Category1__c;
            CompanyBottomPrice =pr != null ?pr.CompanyBottomPrice__c:null;
            HospitalBottomPrice=pr != null ?pr.HospitalBottomPrice__c:null;
            HospitalManagerPrice=pr != null ?pr.HospitalManagerPrice__c:null;
            chk = false;
            isFromProduct = false;
            isFromSystem = isSys;
            zl=cp.Product__r.zl__c;
            typeName = cp.Product__r.TypeName__c;
            Taxrate = pr != null ? pr.Taxrate__c: 0;
            AreaDealerPrice = pr != null ?pr.AreaDealerPrice__c:null;
            parentSalesPrice = sprice;




        }
         public ContractChangeListBean(ContractChangeList__c cp){
        	invoiceDealerPrice = cp.InvoiceDealerPrice__c;
            product = cp.product__c;
            productLine = cp.productLine__c;
           	instCost = cp.instCost__c;
            code = cp.code__c;
            SalesPrice = cp.SalesPrice__c;
            AdjustPrice = cp.AdjustPrice__c;
            AdjustSalesPrice = cp.AdjustSalesPrice__c;
            Company = cp.Company__c;
            Size = cp.Size__c;
            ContractProduct = cp.ContractProduct__c;
            ItemNumber = cp.ItemNumber__c;
            Type = cp.Type__c;
            Brand = cp.Brand__c;
            AreaDealerPrice = cp.AreaDealerPrice__c;
            DelProduct = cp.DelProduct__c;
            swjhdj = cp.swjhdj__c;
            MarketPrice = cp.MarketPrice__c;
            num = cp.Number__c;
            AddProduct = cp.AddProduct__c;
            Model = cp.Model__c;
            productName= cp.Product__r.Name;
            Taxrate = cp.Taxrate__c;
            CompanyBottomPrice =cp.CompanyBottomPrice__c;
            HospitalBottomPrice=cp.HospitalBottomPrice__c;
            HospitalManagerPrice=cp.HospitalManagerPrice__c;
             
            category0 = cp.ContractProduct__r.Product__r.Category0__c;
            category1 = cp.ContractProduct__r.Product__r.Category1__c; 
            chk = false;
            isFromProduct = (cp.ContractProduct__c == null );
            zl=cp.Product__r.zl__c;
            discountRate = cp.DiscountRate__c;
            parentSalesPrice = cp.SystemPrice__c ;
            isFromSystem = cp.IsFromSystem__c;
            
        }
        public ContractChangeListBean(Product__c p,Price__c pr,Decimal sprice){
        	invoiceDealerPrice = pr != null ?pr.InvoiceDealerPrice__c:null;
            product = p.Id;
            productLine = p.Category1__c;
           	instCost = pr != null ? pr.instCost__c:null;
         
          	AdjustPrice = false;
            Size = p.Spec__c;
            ItemNumber = p.SerialNo__c;
            Brand = p.Brand__c;
            AreaDealerPrice = pr != null ?pr.AreaDealerPrice__c:null;
            DelProduct = false;
            swjhdj = p.swjhdj__c;
            MarketPrice = pr != null ?pr.MarketPrice__c:null;
            AddProduct = true;
            Model = p.Model__c;
            productName= p.Name; 
            zl = p.zl__c;
            Taxrate = pr != null ? pr.Taxrate__c: 0;
            category0 = p.Category0__c;
            category1 = p.Category1__c;
            
            CompanyBottomPrice =pr != null ?pr.CompanyBottomPrice__c:null;
            HospitalBottomPrice=pr != null ?pr.HospitalBottomPrice__c:null;
            HospitalManagerPrice=pr != null ?pr.HospitalManagerPrice__c:null;
            parentSalesPrice = sprice;
            
            chk = false;
            isFromProduct = true;
            isFromSystem = false;
            typeName = p.TypeName__c;
        }
         
    }
    public class ProductBean {
		public Product__c rec { get; set; }
        public Price__c pr { get; set; }
        public Boolean chk { get; set; }
        public Decimal salesprice {get;set;}

        public ProductBean(Product__c record, Price__c price,Decimal sprice) {
            rec = record;
            pr = price;
            salesprice = sprice;
            chk = false;
        }
    }*/
}